<?php


namespace app\admin\controller;

use app\admin\model\Admin\AdminModel;
use app\Utils\AliPayUtil;
use app\Utils\Util;
use app\Utils\WxPayUtil;
use think\App;

class AppController extends BaseController
{
    protected $admin;
    protected $menus;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $param = $app->request->param();
        if(!empty($param['access_token'])){
            $data = checkToken($param['access_token']);
            if($data['code'] == 1){
                $array = Util::getModelJson($data['data']);
                $this->admin = AdminModel::getAdminWhere([['id','=',$array['uid']]]);
            }else{
                header('Content-Type:application/json; charset=utf-8');
                echo json_encode(['code'=>self::CODE_LOGIN,'msg'=>$data['msg']]);
                exit();
            }
        }else{
            header('Content-Type:application/json; charset=utf-8');
            echo json_encode(['code'=>self::CODE_LOGIN,'msg'=>"token无效"]);
            exit();
        }
    }

    /**
     * 微信发起支付
     * @param string $type 微信支付类型
     * @param float $total_fee 支付金额
     * @param string $out_trade_no 支付订单号out_trade_no
     * @param string $body 内容
     * @return array
     */
    public function weChatPay($type, $total_fee, $out_trade_no, $body)
    {
        $config = config('weixin.weixin_api');
        $appId = "";
        $secret = "";
        $mchId = "";
        $key = "";
        $notify_url = "";
        if($type == "YE"){
            //余额付款
            return Util::returnJson(self::CODE_SUCCESS,null,'发起支付成功');
        }else{
            $weChat_pay = new WxPayUtil($type, $appId, $secret, $mchId, $key, $out_trade_no, $body, $total_fee * 100, $notify_url);
            $result_pay = $weChat_pay->payOrder();
            if($result_pay['code'] == 0){
                return Util::returnJson(self::CODE_SUCCESS,$result_pay['data'],$result_pay['msg']);
            }else{
                return Util::returnJson(self::CODE_ERROR,null,$result_pay['msg']);
            }
        }
    }

    /**
     * 支付宝发起支付
     * @param float $total_fee 支付金额
     * @param string $out_trade_no 支付订单号out_trade_no
     * @param string $body 内容
     * @param $body
     * @return array
     */
    public function aliPay($total_fee, $out_trade_no, $body)
    {
        $config = config('weixin.weixin_api');
        $appId       = "";
        $private_key = "";
        $public_key  = "";
        $buyer_id    = "";
        $key         = "";
        $notify_url  = "";

        $ali_pay = new AliPayUtil($appId, $private_key,$public_key, $key, $out_trade_no, $body, $total_fee, $notify_url,$buyer_id);
        $result_pay = $ali_pay->payOrder();
        if($result_pay['code'] == 0){
            return Util::returnJson(self::CODE_SUCCESS,$result_pay['data'],$result_pay['msg']);
        }else{
            return Util::returnJson(self::CODE_ERROR,null,$result_pay['msg']);
        }
    }
}